from sklearn.datasets import load_wine
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
wine = load_wine()#导入数据集
data = wine['data']
target = wine['target']
sample=pd.concat([pd.DataFrame(data),pd.DataFrame(target)],axis=1)#用pandas合并数据集
# print(sample.head(3))
alcohol_mean = sample[10].mean()#酒精的指标在第11列，对酒精含量求均值
print("1、计算平均酒精含量:",alcohol_mean)
#数据集划分为训练集，测试集
data_train,data_test,target_train,target_test = train_test_split(data,target,test_size=0.2,random_state=125)
#实现对数据的标准化
print("2、实现对数据的标准化")
stdScaler = StandardScaler().fit(data_train)
data_std_train = stdScaler.transform(data_train)
data_std_test = stdScaler.transform(data_test)
# 使用KNN算法实现红酒的分类功能
print("3、使用KNN算法实现红酒的分类功能")
model=KNeighborsClassifier(n_neighbors=5)
model.fit(data_train,target_train)
pre=model.predict(data_test)
acc=model.score(data_test,target_test)
print("模型在测试集上的精度为:",acc)
